package com.easyplan.planningresultreciver.controller;


import com.alibaba.fastjson2.JSON;
import com.easyplan.planningresultreciver.domain.PlannedResult;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;

@RestController
@RequestMapping(value="/APSEngine")
public class ResultListenerController {

    /**
     * 用于接收规划结果的接口
     * @return
     */
    @RequestMapping(method = RequestMethod.POST, value = "/receive")
    @CrossOrigin
    @ResponseBody
    public void receive(@RequestBody String plannedResultString) {


        System.out.println("[" + this.getNowString() + " INFO ]"  +  "\t请到规划结果反馈.");
        System.out.println(plannedResultString);

        if(plannedResultString == null || plannedResultString == "") {
            System.out.println("[" + this.getNowString() + " INFO ]"  +  "\t接收到请求，但所传参数结果为空，请检查发送端参数.");
        }

        try {
            PlannedResult plannedResult = JSON.parseObject(plannedResultString, PlannedResult.class);
            String targetFile = ResultService.saveResult(plannedResult);
            System.out.println("[" + this.getNowString() + " INFO ]"  +  "\t结果已保存到:" + targetFile + "\n");
        } catch (IOException e) {
            throw new RuntimeException(e);
        }

    }

    private String getNowString() {

            SimpleDateFormat formater = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            String dateNow = formater.format(new Date());
            return dateNow;

    }

}
